﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Text;
using System.IO;
namespace zmMoniter
{
    public partial class frmDBSetting : DevExpress.XtraEditors.XtraForm
    {
        
        public mainForm main = null;
        public frmDBSetting(mainForm frm)
        {
            InitializeComponent();
            this.main = frm;
        }
        private string getIP(string sCon)
        {
            int iPos1 = sCon.IndexOf("HOST=");
            sCon = sCon.Substring(iPos1 + 5, (sCon.Length - iPos1 - 5));
            int iPos2 = sCon.IndexOf(")");
            sCon = sCon.Substring(0,iPos2);
            return sCon;
        }
        private string getPort(string sCon)
        {
            int iPos1 = sCon.IndexOf("PORT=");
            sCon = sCon.Substring(iPos1 + 5, (sCon.Length - iPos1 - 5));
            int iPos2 = sCon.IndexOf(")");
            sCon = sCon.Substring(0,iPos2);
            return sCon;
        }
        private string getService(string sCon)
        {
            int iPos1 = sCon.IndexOf("SID=");
            sCon = sCon.Substring(iPos1 + 4, (sCon.Length - iPos1 - 4));
            int iPos2 = sCon.IndexOf(")");
            sCon = sCon.Substring(0,iPos2);
            return sCon;
        }
        public bool reSetData()
        {
            bool bResult = false;
            getStr();
            if (mainForm.connstring != null && mainForm.connstring != "")
            {
                string[] connStrs = mainForm.connstring.Split(';');
                if (connStrs.Length >= 3)
                {
                    string[] sUserName = connStrs[1].Split('=');
                    string[] sPassWord = connStrs[2].Split('=');
                    tEdt_IP.Text = getIP(connStrs[0]);
                    tEdt_Port.Text = getPort(connStrs[0]);
                    tEdt_ServiceName.Text = getService(connStrs[0]);
                    if (sUserName.Length == 2)
                        tEdt_User.Text = sUserName[1];
                    if (sPassWord.Length == 2)
                        tEdt_Psw.Text = sPassWord[1];
                    bResult = true;
                }
            }
            return bResult;
        }
        private void sBtn_Test_Click(object sender, EventArgs e)
        {
            
            label_Msg.Text = "正在测试...";
            label_Msg.Visible = true;
            string connStr = getConnStr();
            try
            {
                PublicDataCls db = new PublicDataCls(getConnStr());
                string sOpenDBResult = db.OpenConn();
                if (sOpenDBResult != "")
                {
                    label_Msg.Text = sOpenDBResult;
                }
                else
                    label_Msg.Text = "测试成功!";
            }
            catch (Exception exp)
            {
                label_Msg.Text = exp.Message;

            }
           
        }
        private string getConnStr()
        {
            string connStr;
            string sDataSource = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP)(HOST=" + tEdt_IP.Text.Trim()
                + ")(PORT=" + tEdt_Port.Text.Trim() + ")))(CONNECT_DATA=(SID=" + tEdt_ServiceName.Text.Trim() + ")(SERVER=DEDICATED)))";
            connStr = sDataSource + ";User ID="+tEdt_User.Text.Trim()+";Password="+ tEdt_Psw.Text;

            return connStr;
        }
      
        private void sBtn_Save_Click(object sender, EventArgs e)
        {
            mainForm.connstring = getConnStr();
            setStr();
            Hide();
            

        }

        private void frmDBSetting_FormClosed(object sender, FormClosedEventArgs e)
        {
          
        }
        private bool setStr()
        {
            bool bCon = false;
            try
            {
                FileStream aFile = new FileStream(Application.StartupPath + "\\config.ini", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                StreamWriter sw = new StreamWriter(aFile);
                sw.WriteLine("[CONNSTRING]=" + mainForm.connstring);
                sw.WriteLine("[SIP]=" + mainForm.sIP);
                sw.WriteLine("[SPORT]=" + mainForm.sPort);
                sw.Close();
                bCon = true;
            }
            catch (IOException ex)
            {
                bCon = false;
            }
            return bCon;
        }
        private bool getStr()
        {
            bool bCon = false;
            try
            {
                FileStream aFile = new FileStream(Application.StartupPath + "\\config.ini", FileMode.OpenOrCreate, FileAccess.Read);
                StreamReader sr = new StreamReader(aFile);
                string nextLine;
                while ((nextLine = sr.ReadLine()) != null)
                {
                    if (nextLine.IndexOf("[CONNSTRING]") >= 0)
                    {
                        int iPos = nextLine.IndexOf("[CONNSTRING]");

                        mainForm.connstring = nextLine.Substring(iPos + 13, nextLine.Length - 13);
                    }
                   
                }
                sr.Close();
                bCon = true;
            }
            catch (IOException ex)
            {
                bCon = false;
            }
            return bCon;
        }
    }
}